package com.cy.tempflinkcdc.service.sqlgenerator;

  import com.cy.tempflinkcdc.datachange.DataChangeInfo;
  import com.cy.tempflinkcdc.util.JSONObjectUtils;
  import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @author: Zzz
 * @date: 2024/3/25 17:17
 * @description:
 */
@Service("CREATE")
@Slf4j
public class InsertSqlGeneratorServiceImpl extends AbstractSqlGenerator {

    @Override
    public String generatorSql(DataChangeInfo dataChangeInfo) {
        String afterData = dataChangeInfo.getAfterData();
        Map<String, Object> afterDataMap = JSONObjectUtils.JsonToMap(afterData);
        StringJoiner columnSetPart = new StringJoiner(",");
        StringJoiner valuePart = new StringJoiner(",");

        for (String key : afterDataMap.keySet()) {
            Object afterValue = afterDataMap.get(key);
//            if ("create_time".equals(key)){
//                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                afterValue = dateFormat.format(afterValue);
//            }
//            if ("lastTime".equals(key)){
//                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                afterValue = dateFormat.format(afterValue);
//            }
            columnSetPart.add(quoteIdentifier(key));
            valuePart.add(formatValue(afterValue));
        }
//        log.info("columnSetPart : {}", columnSetPart);
//        log.info("valuePart : {}", valuePart);

        return "INSERT INTO " + dataChangeInfo.getTableName() + "(" + columnSetPart + ") VALUES("
                + valuePart + ")@@@@@@;";
    }
}
